home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 34
/
Amiga Format CD34 (1998-11-20)(Future Publishing)(GB)[!][Christmas issue].iso
/
-seriously_amiga-
/
programming
/
c
/
viewperf5.1
/
tools
/
convert.c
next >
Wrap
C/C++ Source or Header
|
1998-10-01
|
30KB
|
1,051 lines
/*
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose and without fee is hereby granted, provided
// that the above copyright notice appear in all copies and that both that
// copyright notice and this permission notice appear in supporting
// documentation, and that the name of I.B.M. not be used in advertising
// or publicity pertaining to distribution of the software without specific,
// written prior permission. I.B.M. makes no representations about the
// suitability of this software for any purpose. It is provided "as is"
// without express or implied warranty.
//
// I.B.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL I.B.M.
// BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// DB Murrell
// * 6/15/93
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/errno.h>
#define VERSION "CONVERT Version 1.0 (6/15/93)"
#define ERROR_OUT stderr
#define END_OF_TEXT "### EOT ###"
#define FLAGS "hH?bBaAvVmMpPoOcC"
#define N_POLY_TYPES 3
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
typedef int int32; /* 32 bit integer */
typedef unsigned int uint32; /* 32 bit unsigned integer */
typedef float float32; /* 32 bit single precision float */
typedef enum {False = 0, True} Boolean;
typedef enum {INFORMATION, WARNING, FATAL} Severity;
typedef enum {BINARY, ASCII} Output_Mode;
typedef enum {MESH, POLYGON} Input_Type;
typedef enum {COORDINATES = 0, ELEMENTS, NORMS} Poly_Type;
static char *self_name = NULL;
/*
* Polygon information table
*/
static struct {
Poly_Type type;
char *ext;
char *desc;
} poly_table[] = {
{ COORDINATES, ".coor", "Coordinate Data" },
{ ELEMENTS, ".elem", "Element Data" },
{ NORMS, ".vnorm", "Vector Normals" }
};
/*
* Help text
*/
static char *help_text[] = {
"Converts viewperf polygon and mesh files to and from binary",
"and ASCII formats",
"",
"USAGE: convert [options] <input_file> <output_file>",
"",
"options:",
" -[hH?] Display this message",
"",
" -[bB] Convert to binary (assumes ASCII input)",
"",
" -[aA] Convert to ASCII (assumes binary input)",
"",
" -[vV] Verbose mode",
"",
" -[mM] Input file is full path to viewperf mesh data",
"",
" -[pPoO] Input file is path prefix to viewperf polygon data",
" (suffixes .coor, .elem, and .vnorm are added by convert)",
"",
" -[cC] Add comments to ASCII output files",
"",
"Input and output files default to stdin, and stdout (respectively) if",
"left unspecified",
END_OF_TEXT
};
/*
********************************************************************
*
* Swap32 -- Byte swap a 32 bit datum to convert between big
* and little endian formats.
*
********************************************************************
*/
static void Swap32(void *src_void, void *dst_void, size_t length)
{
register uint32 tmp;
uint32 *src = (uint32 *) src_void;
uint32 *dst = (uint32 *) dst_void;
size_t i;
for (i = 0; i < length; i++) {
tmp = src[i];
tmp &= 0xffffffff;
tmp = ((tmp >> 24) | (tmp << 24) |
((tmp >> 8) & 0xff00) | ((tmp << 8) & 0xff0000));
dst[i] = tmp;
}
}
/*
********************************************************************
*
* Error -- Error reporting
*
********************************************************************
*/
static void Error(Severity sev, char *file, unsigned line,
char *procedure, char *format, ...)
{
char level[16];
va_list args;
/*
* Sanity check
*/
if (file == NULL || self_name == NULL || procedure == NULL)
return;
va_start(args, format);
switch (sev)
{
case INFORMATION:
strcpy(level, "NOTE");
break;
case WARNING:
strcpy(level, "*** WARNING");
break;
default:
strcpy(level, ">>> FATAL");
break;
}
fprintf(ERROR_OUT, "%s ('%s'@%d, %s::%s) -\n\t",
level, file, line, self_name, procedure);
/*
if (format == NULL)
fprintf(ERROR_OUT, "%s", strerror(errno));
else
vfprintf(ERROR_OUT, format, args);
*/
fprintf(ERROR_OUT, "\n");
fflush(ERROR_OUT);
va_end(args);
if (sev == FATAL)
exit(1);
} /* Error */
/*
********************************************************************
*
* Expand -- Stretch a storage area to at least size (bytes) given
*
********************************************************************
*/
static void Expand(char **store, size_t size)
{
/*
* Sanity check
*/
if (store == NULL || size == 0)
return;
if ((*store) == NULL)
{
if (((*store) = (char *)malloc(size)) == NULL)
Error(FATAL, __FILE__, __LINE__, "Expand", NULL);
}
else if (((*store) = (char *)realloc(*store, size)) == NULL)
Error(FATAL, __FILE__, __LINE__, "Expand", NULL);
} /* Expand */
/*
********************************************************************
*
* Str_Dup -- Generate copy of string argument
*
********************************************************************
*/
static void Str_Dup(char **dest, char *src)
{
if (src == NULL)
{
if (*dest != NULL)
free(*dest);
*dest = NULL;
return;
} /* if */
else if (*dest == NULL)
{
if ((*dest = (char *)malloc(strlen(src) + 1)) == NULL)
Error(FATAL, __FILE__, __LINE__, "Str_Dup", NULL);
} /* else if */
else if ((*dest = (char *)realloc(*dest, strlen(src) + 1)) == NULL)
Error(FATAL, __FILE__, __LINE__, "Str_Dup", NULL);
strcpy(*dest, src);
} /* Str_Dup */
/*
********************************************************************
*
* Str_Append -- Tack a list of strings onto the end of the first,
* expanding the first if necessary.
*
********************************************************************
*/
static void Str_Append(char **dest, ...)
{
va_list args;
char *src;
/*
* Sanity checks
*/
if (dest == NULL)
return;
va_start(args, dest);
while ((src = (char *)va_arg(args, char*)) != NULL)
{
if ((*dest) == NULL)
{
Expand(dest, (strlen(src) + 1) * sizeof(char));
strcpy(*dest, src);
} /* if */
else
{
Expand(dest, (strlen(*dest) + strlen(src) + 1) * sizeof(char));
strcat(*dest, src);
} /* else */
} /* while */
va_end(args);
} /* Str_Append */
/*
********************************************************************
*
* Show_Help_Text -- Text for help message
*
********************************************************************
*/
static void Show_Help_Text(void)
{
register unsigned i = 0;
fprintf(stderr, "\t\t\t%s\n\n", VERSION);
while (strcmp(help_text[i], END_OF_TEXT) != 0)
fprintf(stderr, "%s\n", help_text[i++]);
} /* Show_Help_Text */
/*
********************************************************************
*
* Scan -- scan past comments and white space
*
********************************************************************
*/
static void Scan(FILE *fp)
{
int c;
/*
* Sanity check
*/
if (fp == NULL)
return;
while (!feof(fp))
{
c = fgetc(fp);
if (isspace(c))
continue;
else if (c == '#')
{
while (((c = fgetc(fp)) != EOF) && (c != '\n'))
;
continue;
} /* else if */
else if (c != EOF)
{
ungetc(c, fp);
break;
} /* else */
} /* while */
} /* Scan */
/*
*************************************************************